package org.wenhuayuan.project.main.demo;

import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import org.wenhuayuan.project.main.R;

public class ActionDispatchDemoActivity extends AppCompatActivity {

    LinearLayout liLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_dispatch_demo);
        liLayout = findViewById(R.id.liLayout);

        Log.i("DemoActivity", "onCreate-liLayout.getWidth="+liLayout.getWidth());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("DemoActivity", "onStart-liLayout.getWidth="+liLayout.getWidth());
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("DemoActivity", "onResume-liLayout.getWidth="+liLayout.getWidth());
        Log.i("DemoActivity", "onResume-liLayout.getWidth="+liLayout.getMeasuredWidth());
        liLayout.post(new Runnable() {
            @Override
            public void run() {
                Log.i("DemoActivity", "onResume-post-liLayout.getWidth="+liLayout.getWidth());
            }
        });
        liLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 每次ViewTree发生变化，都会触发监听回调。一共发生了3次。
                // 如果让他发生一次，那么获取到一次回调后，移除监听
                liLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                Log.i("DemoActivity", "onResume-ViewTreeObserver-liLayout.getWidth="+liLayout.getWidth());
            }
        });
    }
}
